<?php
/**
 * CApplication class file.
 * 这是框架所有核心类的基类.
 * @author Huan Song <iphper@yeah.net>
 * @version $Id: CApplication.php 1 2011-01-08 09:16:06Z huan.song $
 * @package system.base
 * @since 2.0
 */
abstract class CApplication 
{	
	/**
	 * 魔术方法: 返回一个受保护的属性值.
	 * 下面情况下调用此方法.
	 * <pre>
	 * $value = $component->propertyName;
	 * </pre>
	 * @param string 受保护的属性名.
	 * @return 受保护的属性值.
	 * @throws CException. 如果受保护的属性名不存在, 抛出CException.
	 * @see __set
	 */
	public function __get($name)
	{
		$getter = 'get' . ucfirst($name);
		if (method_exists($this, $getter))
			return $this->$getter();
		
		throw new CException('CApplication:__get method not exists', array('{class}' => get_class($this), '{method}' => $getter));
	}
	
	/**
	 * 魔术方法: 设置一个受保护的属性值.
	 * 下面情况下调用此方法.
	 * <pre>
	 * $component->propertyName = $value;
	 * </pre>
	 * @param string 受保护的属性名.
	 * @param string|array|etc. 受保护的属性值.
	 * @return void.
	 * @throws CException. 如果受保护的属性名不存在, 抛出CException.
	 * @see __get
	 */
	public function __set($name, $value)
	{
		$setter = 'set' . ucfirst($name);
		if (method_exists($this, $setter))
			return $this->$setter($value);
		
		throw new CException('CApplication:__set method not exists', array('{class}' => get_class($this), '{method}' => $getter));
	}
	
	/**
	 * PDO连接数据库对象.
	 * <pre>
	 * $CDbDriver = $this->getDbDriver();
	 * $CDbDriver = $this->dbDriver;
	 * </pre>
	 * @return CDbDriver PDO连接数据库对象.
	 */
	public function getDbDriver()
	{
		if (!CRegistry::has('CDbDriver'))
		{
			$CDbDriver = new CDbDriver();
			$CDbDriver->active = true;
			CRegistry::set('CDbDriver', $CDbDriver);
		}
		
		return CRegistry::get('CDbDriver');
	}
	
	/**
	 * 数据库操作基类.
	 * <pre>
	 * $CDbStatement = $this->getDbStatement();
	 * $CDbStatement = $this->dbStatement;
	 * </pre>
	 * @return CDbStatement 数据库操作基类.
	 */
	public function getDbStatement()
	{
		if (!CRegistry::has('CDbStatement'))
		{
			$CDbDriver = $this->getDbDriver();
			$CDbStatement = new CDbStatement($CDbDriver);
			CRegistry::set('CDbStatement', $CDbStatement);
		}
		
		return CRegistry::get('CDbStatement');
	}
}
